package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import org.come.entity.TeamRole;
import org.come.until.CutButtonImage;
import org.come.until.FormsManagement;

import com.tool.btn.TeamArenaBtn;
import com.tool.image.ImageMixDeal;
import com.tool.tcp.Sprite;
import com.tool.tcp.SpriteFactory;
import com.tool.tcpimg.UIUtils;

 

/**
 * @author HGC<br>
 * @time 2020年4月21日 上午9:35:21<br>
 * @class 类名:TeamArenaMainJpanel 组队竞技主面板 编号:108<br>
 */
public class TeamArenaMainJpanel extends JPanel {

    private List<TeamArenaModelJPanel> listTeamArenaModel;
    private TeamArenaBtn btnYes, btnNo,btnYes1,btnNo1;
    

    public TeamArenaMainJpanel() {
        setPreferredSize(new Dimension(591, 406));
        setOpaque(false);
        setLayout(null);
        getListTeamArenaModel();
        getBtnNo();
        getBtnYes();
        getBtnYes1();
        getBtnNo1();
    }
    public TeamArenaBtn getBtnYes1() {
        if (btnYes1 == null) {
            btnYes1 = new TeamArenaBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "匹配", 3,
                    this);
            btnYes1.setBounds(82, 177, 59, 24);
            btnYes1.setVisible(false);
            this.add(btnYes1);
        }
        return btnYes1;
    }

    public TeamArenaBtn getBtnNo1() {
        if (btnNo1 == null) {
            btnNo1 = new TeamArenaBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "拒绝", 4,
                    this);
            btnNo1.setBounds(447, 177, 59, 24);
            btnNo1.setVisible(false);
            this.add(btnNo1);
        }
        return btnNo1;
    }
    /**按钮展示*/
    public void btnShow(int type, Integer typesOfEvents){
        if(type == 0){

            if(typesOfEvents == 1){
                btnNo.setText("拒绝");
                btnYes.setText("匹配");
                btnNo.setVisible(true);
                btnYes.setVisible(true);
                btnNo1.setVisible(false);
                btnYes1.setVisible(false);


            }else if (typesOfEvents==2) {
                btnNo1.setText("拒绝");
                btnYes1.setText("匹配");
                btnNo1.setVisible(true);
                btnYes1.setVisible(true);
                btnNo.setVisible(false);
                btnYes.setVisible(false);
            }

        }else if(type == 1){
            if(typesOfEvents == 1){
                btnNo.setVisible(false);
                btnYes.setVisible(false);
            }else if(typesOfEvents == 2){
                btnNo1.setVisible(false);
                btnYes1.setVisible(false);
            }

        }else if(type == 2){

            if(typesOfEvents == 1){
                btnNo.setVisible(true);
                btnNo.setText("取消");
                btnYes.setVisible(false);
            }else if(typesOfEvents == 2){
                btnNo1.setVisible(true);
                btnNo1.setText("取消");
                btnYes1.setVisible(false);
            }
        }
    }
    /**隐藏面板*/
    public void hideShow(){
        typeCount=0;
        buffer.setLength(0);
        FormsManagement.HideForm(108);
        for (int i = 0; i < listTeamArenaModel.size(); i++) {
            listTeamArenaModel.get(i).showRole(null);
        }
    }
    /**同意*/
    public void addCount(BigDecimal decimal){
        typeCount++;
        for (int i = 0; i < listTeamArenaModel.size(); i++) {
            TeamArenaModelJPanel teamArenaModelJPanel = listTeamArenaModel.get(i);
            if(teamArenaModelJPanel.getTeamRoleId()!= null &&teamArenaModelJPanel.getTeamRoleId().compareTo(decimal) == 0){
                teamArenaModelJPanel.setTcpType(1);
            }
        }
        if(typeCount==teamNum){
            changeCountDown(null, 0, 1);
        }
    }
    
    /**展示自己或者地方队伍 0是敌方 1是自己*/
    public void showMine(List<TeamRole> teamRoles,int numType){
        int num = numType*5;
        if(teamRoles != null && teamRoles.size()!= 0){
            int size = teamRoles.size();
            for (int i = 0,len = listTeamArenaModel.size(); i < len; i++) {
                if(i>=5){
                    break;
                }
                if(i<size){
                    TeamRole teamRole = teamRoles.get(i);
                    if(i<5){
                        listTeamArenaModel.get(i+num).showRole(teamRole);
                    }
                }else{
                    listTeamArenaModel.get(i+num).showRole(null);
                }
            }
        }
        if(numType == 1){
            changeCountDown("倒计时", 30,1);
            teamNum = teamRoles.size();
        }else if(numType == 0){
            btnShow(1,-1);
            changeCountDown(null, 0,0);
        }
    }
    
    /**改变倒计时*/
    public void changeCountDown(String str,int countDown,int typeMenu){
        this.countDown = countDown;
        strMatch = str;
        this.typeMenu = typeMenu;
    }

    private ImageIcon icon;
    private ImageIcon iconIco;
    private String strMatch;
    //countDown计时时间，timeNum每隔多久跳一次,typeMenu展示东西(1倒计时展示0挑战图片),xNum展示位置，typeCount同意匹配个数
    private int countDown, timeNum=60, typeMenu = 1,xNum=287,typeCount=0,teamNum = 0;
    private StringBuffer buffer = new StringBuffer();
    private Sprite tcp = SpriteFactory.VloadSprite("resource/mouse/teamArenaBack.tcp", null);

    /**匹配时间计算展示*/
    public void getTime() {
        countDown++;
        int min = countDown / 60;
        int sec = countDown % 60;
        buffer.setLength(0);
        if (min > 0) {
            buffer.append(min);
        } else {
            buffer.append("0");
        }
        buffer.append(":");
        if (sec > 9) {
            buffer.append(sec);
        } else {
            buffer.append("0");
            buffer.append(sec);
        }
    }
    /**倒计时*/
    public void getCountDown(){
        buffer.setLength(0);
        buffer.append(strMatch);
        buffer.append("0:");
        if (countDown > 9) {
            buffer.append(countDown);
        } else {
            buffer.append("0");
            buffer.append(countDown);
        }
        if(countDown>0) {
            countDown--;
        }
    }
    
    /**展示倒计时*/
    private void grawTime(Graphics g) {
        g.setColor(Color.white);
        g.setFont(UIUtils.TEXT_FONT22);
        timeNum++;
        if(buffer.length()!=0) {
            g.drawString(buffer.toString(), xNum, 199);
        }
        if (timeNum > 55) {
            if (strMatch == null) {
                getTime();
            }else{
                getCountDown();
            }
            int canDisplayUpTo = g.getFontMetrics().stringWidth(buffer.toString());
            xNum = 287 - canDisplayUpTo/2;
            timeNum = 0;
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (icon == null) {
            icon = new ImageIcon("inkImg/old/background/S1912.png");
        }
        g.drawImage(icon.getImage(), 0, 0, null);
        if (iconIco == null) {
            iconIco = CutButtonImage.getImage("inkImg/old/background/S192.png", -1, -1);
        }
        if (typeMenu == 0) {
            g.drawImage(iconIco.getImage(), 258, 169, null);
        } else {
            grawTime(g);
        }
    }
    @Override
    protected void paintChildren(Graphics g) {
        super.paintChildren(g);
        tcp.updateToTime(ImageMixDeal.userimg.getTime(), 0);
        for (int i = 0,len =listTeamArenaModel.size(); i < len; i++) {
            if(typeMenu == 0){
                if(listTeamArenaModel.get(i).getTeamRoleId() !=null) {
                    tcp.draw(g, 50+17 + i % 5 * 96, 57 + i / 5 * 157);
                }
            }else{
                if (listTeamArenaModel.get(i).getTcpType() == 1) {
                    tcp.draw(g, 50+17 + i % 5 * 96, 57 + i / 5 * 157);
                }
            }
        }
    }

    public List<TeamArenaModelJPanel> getListTeamArenaModel() {
        if (listTeamArenaModel == null) {
            listTeamArenaModel = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                TeamArenaModelJPanel arenaModelJPanel = new TeamArenaModelJPanel();
                arenaModelJPanel.setBounds(50 + i % 5 * 96, 59 + i / 5 * 157, 93, 110);
                this.add(arenaModelJPanel);
                listTeamArenaModel.add(arenaModelJPanel);
            }
        }
        return listTeamArenaModel;
    }

    public void setListTeamArenaModel(List<TeamArenaModelJPanel> listTeamArenaModel) {
        this.listTeamArenaModel = listTeamArenaModel;
    }

    public TeamArenaBtn getBtnYes() {
        if (btnYes == null) {
            btnYes = new TeamArenaBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "匹配", 1,
                    this);
            btnYes.setBounds(82, 177, 59, 24);
            this.add(btnYes);
        }
        return btnYes;
    }

    public void setBtnYes(TeamArenaBtn btnYes) {
        this.btnYes = btnYes;
    }

    public TeamArenaBtn getBtnNo() {
        if (btnNo == null) {
            btnNo = new TeamArenaBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "拒绝", 2,
                    this);
            btnNo.setBounds(447, 177, 59, 24);
            this.add(btnNo);
        }
        return btnNo;
    }

    public void setBtnNo(TeamArenaBtn btnNo) {
        this.btnNo = btnNo;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    public ImageIcon getIconIco() {
        return iconIco;
    }

    public void setIconIco(ImageIcon iconIco) {
        this.iconIco = iconIco;
    }

    public String getStrMatch() {
        return strMatch;
    }

    public void setStrMatch(String strMatch) {
        this.strMatch = strMatch;
    }

    public int getTimeNum() {
        return timeNum;
    }

    public void setTimeNum(int timeNum) {
        this.timeNum = timeNum;
    }

    public int getTypeMenu() {
        return typeMenu;
    }

    public void setTypeMenu(int typeMenu) {
        this.typeMenu = typeMenu;
    }

    public int getxNum() {
        return xNum;
    }

    public void setxNum(int xNum) {
        this.xNum = xNum;
    }

    public StringBuffer getBuffer() {
        return buffer;
    }

    public void setBuffer(StringBuffer buffer) {
        this.buffer = buffer;
    }

    public void setCountDown(int countDown) {
        this.countDown = countDown;
    }
    public int getTypeCount() {
        return typeCount;
    }
    public void setTypeCount(int typeCount) {
        this.typeCount = typeCount;
    }
    public int getTeamNum() {
        return teamNum;
    }
    public void setTeamNum(int teamNum) {
        this.teamNum = teamNum;
    }
    public Sprite getTcp() {
        return tcp;
    }
    public void setTcp(Sprite tcp) {
        this.tcp = tcp;
    }
    
}
